﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Portals;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class ManagerMenuController : BasePortController
    {
        public override string ExportModule(int ModuleID)
        {
            return this.ExportModuleData(ModuleID, null, null, null, true, true);
        }

        public string ExportModuleData(int ModuleID, CheckBoxList chkSettingsList, CheckBoxList chkTemplateList, CheckBoxList chkLangList, bool ExportStatusCodes, bool ExportReports)
        {
            string str2 = "";
            PortalSettings currentPortalSettings = PortalController.GetCurrentPortalSettings();
            Export export = new Export();
            Hashtable languageList = new Hashtable();
            if (chkLangList == null)
            {
                languageList.Add(SharedFunctions.GetCurrentCulture(), SharedFunctions.GetCurrentCulture());
            }
            else
            {
                IEnumerator enumerator;
                try
                {
                    enumerator = chkLangList.Items.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        ListItem current = (ListItem) enumerator.Current;
                        if (current.Selected)
                        {
                            languageList.Add(current.Value, current.Value);
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            str2 = (str2 + "<storecontent>") + export.GetSettingsXML(currentPortalSettings.PortalId, chkSettingsList, languageList) + export.GetSettingsTextXML(currentPortalSettings.PortalId, chkTemplateList, languageList);
            if (ExportStatusCodes)
            {
                str2 = str2 + export.GetStatusXML();
            }
            if (ExportReports)
            {
                str2 = str2 + export.GetSQLReports(currentPortalSettings.PortalId);
            }
            return (str2 + "</storecontent>");
        }

        public override void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {
            this.ImportModuleData(ModuleID, Content, Version, UserId, null, null, null);
        }

        public void ImportModuleData(int ModuleID, string Content, string Version, int UserId, CheckBoxList chkSettingsList, CheckBoxList chkTemplateList, CheckBoxList chkLangList)
        {
            int portalId;
            XmlNode node;
            XmlNode content = DotNetNuke.Common.Globals.GetContent(Content, "storecontent");
            OrderController controller2 = new OrderController();
            bool forcedOverwrite = false;
            ModuleInfo module = new ModuleController().GetModule(ModuleID, -1);
            if (module == null)
            {
                portalId = PortalController.GetCurrentPortalSettings().PortalId;
            }
            else
            {
                portalId = module.PortalID;
            }
            Hashtable hashtable = new Hashtable();
            if (chkLangList == null)
            {
                hashtable.Add(SharedFunctions.GetCurrentCulture(), SharedFunctions.GetCurrentCulture());
            }
            else
            {
                IEnumerator enumerator;
                try
                {
                    enumerator = chkLangList.Items.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        ListItem current = (ListItem) enumerator.Current;
                        if (current.Selected)
                        {
                            hashtable.Add(current.Value, current.Value);
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            XmlNode xmlInput = content.SelectSingleNode("settings");
            if (xmlInput != null)
            {
                IEnumerator enumerator2;
                try
                {
                    enumerator2 = xmlInput.SelectNodes("*").GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        node = (XmlNode) enumerator2.Current;
                        NB_Store_SettingsInfo objSInfo = new NB_Store_SettingsInfo {
                            PortalID = portalId,
                            Lang = node.Attributes["Lang"].Value,
                            HostOnly = Conversions.ToBoolean(node.Attributes["HostOnly"].Value),
                            SettingName = node.Name,
                            SettingValue = xmlInput.SelectSingleNode(node.Name + "[@Lang=\"" + node.Attributes["Lang"].Value + "\"]").InnerText
                        };
                        if (node.Attributes["CtrlType"] != null)
                        {
                            objSInfo.CtrlType = node.Attributes["CtrlType"].Value;
                        }
                        else
                        {
                            objSInfo.CtrlType = "";
                        }
                        if (node.Attributes["GroupRef"] != null)
                        {
                            objSInfo.GroupRef = node.Attributes["GroupRef"].Value;
                        }
                        else
                        {
                            objSInfo.GroupRef = "";
                        }
                        if (node.Attributes["HostOnly"] != null)
                        {
                            objSInfo.HostOnly = Conversions.ToBoolean(node.Attributes["HostOnly"].Value);
                        }
                        else
                        {
                            objSInfo.HostOnly = Conversions.ToBoolean("");
                        }
                        if (node.Attributes["Overwrite"] != null)
                        {
                            forcedOverwrite = Conversions.ToBoolean(node.Attributes["Overwrite"].Value);
                        }
                        else
                        {
                            forcedOverwrite = false;
                        }
                        if (chkSettingsList == null)
                        {
                            this.updateSettingData(portalId, objSInfo, forcedOverwrite);
                        }
                        else if ((chkSettingsList.Items.FindByValue(objSInfo.SettingName) != null) && (chkSettingsList.Items.FindByValue(objSInfo.SettingName).Selected & hashtable.Contains(objSInfo.Lang.Trim())))
                        {
                            this.updateSettingData(portalId, objSInfo, forcedOverwrite);
                        }
                    }
                }
                finally
                {
                    if (enumerator2 is IDisposable)
                    {
                        (enumerator2 as IDisposable).Dispose();
                    }
                }
            }
            xmlInput = content.SelectSingleNode("settingstext");
            if (xmlInput != null)
            {
                IEnumerator enumerator3;
                try
                {
                    enumerator3 = xmlInput.SelectNodes("*").GetEnumerator();
                    while (enumerator3.MoveNext())
                    {
                        node = (XmlNode) enumerator3.Current;
                        NB_Store_SettingsTextInfo objSTInfo = new NB_Store_SettingsTextInfo {
                            PortalID = portalId,
                            Lang = node.Attributes["Lang"].Value,
                            SettingName = node.Name,
                            SettingText = xmlInput.SelectSingleNode(node.Name + "[@Lang=\"" + node.Attributes["Lang"].Value + "\"]").InnerText
                        };
                        if (node.Attributes["CtrlType"] != null)
                        {
                            objSTInfo.CtrlType = node.Attributes["CtrlType"].Value;
                        }
                        else
                        {
                            objSTInfo.CtrlType = "";
                        }
                        if (node.Attributes["GroupRef"] != null)
                        {
                            objSTInfo.GroupRef = node.Attributes["GroupRef"].Value;
                        }
                        else
                        {
                            objSTInfo.GroupRef = "";
                        }
                        if (node.Attributes["HostOnly"] != null)
                        {
                            objSTInfo.HostOnly = Conversions.ToBoolean(node.Attributes["HostOnly"].Value);
                        }
                        else
                        {
                            objSTInfo.HostOnly = false;
                        }
                        if (node.Attributes["Overwrite"] != null)
                        {
                            forcedOverwrite = Conversions.ToBoolean(node.Attributes["Overwrite"].Value);
                        }
                        else
                        {
                            forcedOverwrite = false;
                        }
                        if (chkTemplateList == null)
                        {
                            this.updateSettingTextData(portalId, objSTInfo, forcedOverwrite);
                        }
                        else if ((chkTemplateList.Items.FindByValue(objSTInfo.SettingName) != null) && (chkTemplateList.Items.FindByValue(objSTInfo.SettingName).Selected & hashtable.Contains(objSTInfo.Lang.Trim())))
                        {
                            this.updateSettingTextData(portalId, objSTInfo, forcedOverwrite);
                        }
                    }
                }
                finally
                {
                    if (enumerator3 is IDisposable)
                    {
                        (enumerator3 as IDisposable).Dispose();
                    }
                }
            }
            xmlInput = content.SelectSingleNode("status");
            if (xmlInput != null)
            {
                IEnumerator enumerator4;
                try
                {
                    enumerator4 = xmlInput.SelectNodes("*").GetEnumerator();
                    while (enumerator4.MoveNext())
                    {
                        node = (XmlNode) enumerator4.Current;
                        NB_Store_OrderStatusInfo objInfo = new NB_Store_OrderStatusInfo {
                            OrderStatusID = Conversions.ToInteger(Strings.Replace(node.Name, "ID", "", 1, -1, CompareMethod.Binary)),
                            Lang = node.Attributes["Lang"].Value,
                            OrderStatusText = xmlInput.SelectSingleNode(node.Name + "[@Lang=\"" + node.Attributes["Lang"].Value + "\"]").InnerText,
                            ListOrder = Conversions.ToInteger(node.Attributes["ListOrder"].Value)
                        };
                        controller2.UpdateObjOrderStatus(objInfo);
                    }
                }
                finally
                {
                    if (enumerator4 is IDisposable)
                    {
                        (enumerator4 as IDisposable).Dispose();
                    }
                }
            }
            xmlInput = content.SelectSingleNode("SQLReports");
            if (xmlInput != null)
            {
                new Import().ImportSQLReports(portalId, xmlInput, false);
            }
        }

        private void updateSettingData(int PortalID, NB_Store_SettingsInfo objSInfo, bool ForcedOverwrite)
        {
            SettingsController controller = new SettingsController();
            if (SharedFunctions.GetStoreSettingBoolean(PortalID, "settings.overwrite", "None") | ForcedOverwrite)
            {
                controller.UpdateObjSetting(objSInfo);
            }
            else
            {
                NB_Store_SettingsInfo info = controller.GetSetting(PortalID, objSInfo.SettingName, objSInfo.Lang);
                if (info == null)
                {
                    controller.UpdateObjSetting(objSInfo);
                }
                else if (info.Lang.Trim(new char[] { ' ' }) != objSInfo.Lang.Trim(new char[] { ' ' }))
                {
                    controller.UpdateObjSetting(objSInfo);
                }
            }
        }

        private void updateSettingTextData(int PortalID, NB_Store_SettingsTextInfo objSTInfo, bool ForcedOverwrite)
        {
            SettingsController controller = new SettingsController();
            if (SharedFunctions.GetStoreSettingBoolean(PortalID, "settings.overwrite", "None") | ForcedOverwrite)
            {
                controller.UpdateObjSettingsText(objSTInfo);
            }
            else
            {
                NB_Store_SettingsTextInfo info = controller.GetSettingsText(PortalID, objSTInfo.SettingName, objSTInfo.Lang);
                if (info == null)
                {
                    controller.UpdateObjSettingsText(objSTInfo);
                }
                else if (info.Lang.Trim(new char[] { ' ' }) != objSTInfo.Lang.Trim(new char[] { ' ' }))
                {
                    controller.UpdateObjSettingsText(objSTInfo);
                }
            }
        }
    }
}

